#include <math.h>

#include "calculateindices.h"
#include "mainwindow.h"


calculateIndices* calculateIndices::instance = NULL;


calculateIndices::calculateIndices():intCont(0),ISE(0),IAE(0),ITAE(0),ITSE(0),intY(0),desvioControl(0),tempIndices(0)
{
    ISECurve.setPen(QPen(Qt::black));
    ISECurve.attach(MainWindow::qwtPlotInd);

    IAECurve.setPen(QPen(Qt::magenta));
    IAECurve.attach(MainWindow::qwtPlotInd);

    ITAECurve.setPen(QPen(Qt::red));
    ITAECurve.attach(MainWindow::qwtPlotInd);

    ITSECurve.setPen(QPen(Qt::blue));
    ITSECurve.attach(MainWindow::qwtPlotInd);


    VYCurve.setPen(QPen(Qt::green));
    VYCurve.attach(MainWindow::qwtPlotInd);

    VSCCurve.setPen(QPen(Qt::cyan));
    VSCCurve.attach(MainWindow::qwtPlotInd);
    running = false;
    K = 0;
    desvioY = 0;

}

calculateIndices::~calculateIndices()
{
    instance = NULL;
}


calculateIndices *calculateIndices::getInstance()
{
    if (instance == NULL)
        instance = new calculateIndices();
    return instance;
}

void calculateIndices::calcStep() {
//    printf("CALCULANDO INDICE\n");
    double erroQuad = pow(pid::erroPast,2);
    double modErro = fabs (pid::erroPast);
    double yAverage = 0;
    double controlAverage = 0;
    //printf("time %f\n",tempIndices);
    tempIndices -= 0.1;
    if(tempIndices <= 0){
        if(running == true){
            emit endCalculate();
            running = false;
        }
        return;
    }
    TimePoints.push_back(K*0.1);
    K+=1;
    ISE += erroQuad;
    IAE += modErro;
    ITAE += modErro * K*0.1;
    ITSE += erroQuad * K*0.1;
    intY += pid::y;
    intCont += pid::controlSignal;
    yAverage =  intY/K;
    controlAverage = intCont/K;
 //   printf("MOD ERRO = %f K = %i TEMPO = %f ITAE = %f\n",modErro,K,K*0.1,ITAE);


    desvioY += pow((pid::y - yAverage),2);
    yVariance = desvioY / K;
    desvioControl += pow((pid::controlSignal - controlAverage),2);

    controlVariance = desvioControl/K;
    //Adicionando Pontos
    ISEPoints.push_back(ISE);
    IAEPoints.push_back(IAE);
    ITAEPoints.push_back(ITAE);
    ITSEPoints.push_back(ITSE);
    VYPoints.push_back(yVariance);
    VSCPoints.push_back(controlVariance);

    ISECurve.setData(TimePoints,ISEPoints);
    IAECurve.setData(TimePoints,IAEPoints);
    ITAECurve.setData(TimePoints,ITAEPoints);
    ITSECurve.setData(TimePoints,ITSEPoints);
    VYCurve.setData(TimePoints,VYPoints);
    VSCCurve.setData(TimePoints,VSCPoints);
    MainWindow::qwtPlotInd->replot();
}

void calculateIndices::setTime(int time) {
        ISEPoints.clear();
        TimePoints.clear();
        IAEPoints.clear();
        ITAEPoints.clear();
        ITSEPoints.clear();
        VYPoints.clear();
        VSCPoints.clear();
        intCont = ISE = IAE = ITAE = ITSE = desvioY = desvioControl = intY = K = 0;
        printf("START INDICES %f\n",intCont);
        tempIndices = time + 0.2;
        running = true;
}



